layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	var newsData = '',keyword='';
	function initPage(){
		//加载页面数据
		$.get(ctx+"/site/getSiteList.json?keyword="+keyword, function(data){
			if(!data.code&&data.code==0){
				newsList(data.data);
			}else{
				 layer.msg('请求失败，请刷新页面重试', {icon: 5,time: 1500}); 
			}
		})
	}
	initPage();

	//查询
	$(".search_btn").click(function(){
		var newArray = [];
		keyword = $(".search_input").val();
			var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
			$.ajax({
				url : ctx+'/site/getSiteList.json',
				data : {keyword:keyword},
				dataType : 'json',
				type : 'post',
				success : function(data){
					if(!data.code&&data.code==0){
						newsList(data.data);
						layer.close(index);
					}else{
						 layer.msg('请求失败，请刷新页面重试', {icon: 5,time: 1500}); 
					}
				}
			});
			
	})
	//操作
	$("body").on("click",".news_edit",function(){  //编辑
		layer.alert('功能暂未开放',{icon:6, title:'编辑'});
	})
	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});
	$("body").on("click",".news_collect",function(index){  //收藏.
		var _this = $(this);id=_this.attr('id');
		layer.confirm('确定初始化栏目吗？不可逆转', {
			  btn: ['确定', '取消'] //可以无限个按钮
			}, function(index, layero){
				var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
				$.ajax({
					url:ctx+'/chnnel/initChennel',
					data : {id:id},
					type:'post',
					dataType:'json',
					success:function(data){
						if(data.code===0){
							_this.removeClass('news_collect').addClass('layui-btn-warm').html("<i class='layui-icon'>&#xe618;</i>已初始化");
							layer.msg('完成', {icon: 1});
						}else{
							layer.msg(data.message, {icon: 2});
						}
					}
					
				});
			});
	})

	function newsList(that){
		//渲染数据
		var renderDate = function (currData){
			var dataHtml = '';
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					dataHtml += '<tr>'
			    	+'<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+'<td align="left">'+currData[i].siteName+'</td>'
			    	+'<td>'+currData[i].domain+'</td>'
			    	+'<td style="color:#f00">'+currData[i].protocol+'</td>'
			    	+'<td>'+currData[i].tplSolution+'</td>'
			    	+'<td>'+currData[i].staticSuffix+'</td>'
			    	+'<td>'+currData[i].dynamicSuffix+'</td>';
			    	dataHtml +=currData[i].type==0?'<td>公共</td>':'<td>私有</td>';
			    	dataHtml +=currData[i].status==0?'<td>启用</td>':'<td>关闭</td>';
			    	dataHtml +='<td>'
					+  '<a class="layui-btn layui-btn-mini news_edit"><i class="iconfont icon-edit"></i> 编辑</a>';
			    	/*	if(!currData[i].isInitChnnel){
			    		dataHtml += '<a class="layui-btn layui-btn-normal layui-btn-mini news_collect" id="'+currData[i].siteId+'"><i class="layui-icon">&#xe62a;</i>初始化栏目</a>';
			    	}else{
			    		dataHtml += '<a class="layui-btn layui-btn-normal layui-btn-mini layui-btn-warm"><i class="layui-icon">&#xe618;</i>已初始化</a>';
			    	}*/	
		    	dataHtml += '<a class="layui-btn layui-btn-danger layui-btn-mini news_del" data-id="'+currData[i].siteId+'"><i class="layui-icon">&#xe640;</i> 删除</a>'
			        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
			}
		    return dataHtml;
		}
		//分页
		if(that)newsData = that.list;
		laypage({
			cont : "page",
			pages : that.pages,
			jump : function(obj,first){
				 if(!first){
					 $.get(ctx+"/site/getSiteList.json?pageNum="+obj.curr+"&keyword="+keyword, function(data){
							if(!data.code&&data.code==0){
								$(".news_content").html(renderDate(data.data.list));
								$('.news_list thead input[type="checkbox"]').prop("checked",false);
						    	form.render();
							}else{
								 layer.msg('请求失败，请刷新页面重试', {icon: 5,time: 1500}); 
							}
						}) 
				 }else{
						$(".news_content").html(renderDate(newsData));
						$('.news_list thead input[type="checkbox"]').prop("checked",false);
						form.render();
				 }
			}
		})
	}
})
